import {observable, action} from 'mobx';
import {WeaLocaleProvider} from 'ecCom';
import * as API_WFREPORT from '../../apis/efficiencyReport';
import * as ReportUtil from '../../util/reportUtil';

const getLabel = WeaLocaleProvider.getLabel;

export class WfTypeAnalyseStore {
    @observable loading = false;
    iswf = false;
    @observable advanceSearchData = {};
    @observable advanceSearchPara = {
        dateType: 1,
        startDate: '',
        endDate: '',
        orgtype: 2,
        orgid: -1,
        wftypeid: '',
        wftypename: ''
    };
    @observable analyseData = {};
    @observable modalParams = {};

    constructor() {
        this.initDatas = this.initDatas.bind(this);
        this.updateAdvanceSearchPara = this.updateAdvanceSearchPara.bind(this);
        this.loadAnalyseData = this.loadAnalyseData.bind(this);
        this.reSet = this.reSet.bind(this);
        this.showModal = this.showModal.bind(this);
        this.updateModalPara = this.updateModalPara.bind(this);
        this.closeModal = this.closeModal.bind(this);
        this.goBack = this.goBack.bind(this);
        this.clearStatus = this.clearStatus.bind(this);
    }

    @action
    initDatas() {
        API_WFREPORT.getWfTypeAnalyseSearchCondition({}).then((data) => {
            this.advanceSearchData = data;
            const {noRight = false, rightKey = '-100',orgid = ''} = data;
            if (!noRight) {
                this.updateAdvanceSearchPara({
                    rightKey,
                    orgid:orgid||''
                });
            }
        });
    }

    loadAnalyseData() {
        this.loading = true;
        Promise.all([
            API_WFREPORT.getWfTypeAnalyseCountData({...this.advanceSearchPara}).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            }),
            API_WFREPORT.getWfTypeAnalyseEchartData({...this.advanceSearchPara}).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            })
        ]).then(result => {
            this.loading = false;
        });
    }

    updateAdvanceSearchPara(para) {
        let isload = true;
        let defaultInfo = {};
        for (let o in para) {
            if (this.advanceSearchPara[o] != para[o]) {
                if (o === 'orgtype') {
                    const orgtype = para[o];
                    if (orgtype > 1) {
                        const {config = []} = this.advanceSearchData;
                        if (config.length === 2) {
                            const {selectLinkageDatas = {}} = config[1];
                            defaultInfo.orgid = ReportUtil.getDefaultId(orgtype, selectLinkageDatas);
                        }
                    }
                }
                if (o === 'dateType') {
                    if (para[o] == 6) {
                        isload = false;
                    } else {
                        defaultInfo.startDate = '';
                        defaultInfo.endDate = '';
                    }
                }
                if ((o === 'orgid' || o === 'startDate') && para[o] == '') {
                    isload = false;
                }
            }
        }
        this.advanceSearchPara = {...this.advanceSearchPara, ...para, ...defaultInfo};
        if (isload) {
            this.loadAnalyseData();
        }
    }

    reSet() {
        this.loading = false;
        this.iswf = false;
        this.advanceSearchPara = {
            dateType: 1,
            startDate: '',
            endDate: '',
            orgtype: 2,
            orgid: -1,
            wftypeid: '',
            wftypename: ''
        };
    }

    //type 1:更多 2:reqlist 3:count
    showModal(type, para = {}) {
        if (type == 1) {
            const dataParams = {
                'api': 'getWfTypeAnalyseEchartMore',
                ...this.advanceSearchPara
            }
            const modalTitle = this.iswf ? getLabel(131925, '流程创建数量排名') : getLabel(132136, '流程类型创建数量排名');
            this.modalParams = {
                modalVisible: true,
                modaltitle: modalTitle,
                isRowClick: true,
                dataParams
            }
        } else if (type == 2) {
            const {key = '', title = ''} = para;
            if (this.iswf) {
                const dataParams = {
                    'api': 'getWfTypeAnalyseRequestList',
                    ...this.advanceSearchPara,
                    workflowid: key
                }
                const modalTitle = getLabel(383302, '具体流程清单');
                this.modalParams = {
                    modalVisible: true,
                    modaltitle: modalTitle,
                    dataParams,
                    needAdvacnceSearch: true
                }
            } else {
                this.iswf = true;
                this.updateAdvanceSearchPara({wftypeid: key, wftypename: title});
            }
        } else if (type == 3) {
            const dataParams = {
                'api': 'getWfTypeAnalyseRequestList',
                ...this.advanceSearchPara,
                ...para
            }
            const modalTitle = getLabel(383302, '具体流程清单');
            this.modalParams = {
                modalVisible: true,
                modaltitle: modalTitle,
                dataParams,
                needAdvacnceSearch: true
            }
        }
    }

    updateModalPara(para = {}) {
        const {keyid = '', title = ''} = para;
        if (this.iswf) {
            const dataParams = {
                'api': 'getWfTypeAnalyseRequestList',
                ...this.advanceSearchPara,
                workflowid: keyid
            }
            const modalTitle = getLabel(383302, '具体流程清单');
            this.modalParams = {
                modalVisible: true,
                modaltitle: modalTitle,
                dataParams,
                needAdvacnceSearch: true
            }
        } else {
            this.closeModal();
            this.iswf = true;
            this.updateAdvanceSearchPara({wftypeid: keyid, wftypename: title});
        }
    }

    closeModal() {
        this.modalParams = {modalVisible: false};
    }

    goBack() {
        this.iswf = false;
        this.updateAdvanceSearchPara({wftypeid: '', wftypename: ''});
    }

    clearStatus() {
        this.loading = false;
        this.iswf = false;
        this.advanceSearchPara = {
            dateType: 1,
            startDate: '',
            endDate: '',
            orgtype: 2,
            orgid: -1,
            wftypeid: '',
            wftypename: ''
        };
        this.advanceSearchData = {};
        this.analyseData = {};
        this.modalParams = {};
    }
}